﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using MediaController.Extractor;

namespace MediaController.Configuration
{
    public class WatchManager
    {
        #region Provate Methods
        private readonly ISynchronizeInvoke _iSynchronizeInvoke;
        private readonly List<FileSystemWatcher> _folderListeners = new List<FileSystemWatcher>();
        //private readonly RarExtractor _extractor;
        #endregion

        public event ProgressChangedEventHandler ProgressChanged;

        public event Action<string> Aborted;

        public event DataReceivedHandler StdErrReceived;

        public event DataReceivedHandler StdOutReceived;

        public WatchManager(ISynchronizeInvoke iSynchronizeInvoke)
        {
            _iSynchronizeInvoke = iSynchronizeInvoke;
            //_extractor = new RarExtractor();

        }

        public void Start()
        {
            foreach (var folderConfiguration in MediaControllerConfig.ControllerConfig.Watch.Folders)
            {
              
            }
        }

        public void Stop()
        {
            //_extractor.();
        }

        #region private Methods

        void WatcherCreated(object sender, FileSystemEventArgs e)
        {
            lock (typeof(FileSystemWatcher))
            {
                //_extractor.Extract(e.FullPath);
            }
        }

        void ExtractorStdOutReceived(object sender, DataReceivedEventArgs e)
        {
            InvokeStdOutReceived(e);
        }

        void ExtractorStdErrReceived(object sender, DataReceivedEventArgs e)
        {
            InvokeStdErrReceived(e);
        }

        void ExtractorAborted(string obj)
        {
            InvokeAborted(obj);
        }

        void ExtractorProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            InvokeProgressChanged(e);
        }

        private void InvokeProgressChanged(ProgressChangedEventArgs e)
        {
            ProgressChangedEventHandler changed = ProgressChanged;
            if (changed != null) changed(this, e);
        }

        private void InvokeAborted(string obj)
        {
            Action<string> aborted = Aborted;
            if (aborted != null) aborted(obj);
        }

        private void InvokeStdOutReceived(DataReceivedEventArgs e)
        {
            DataReceivedHandler received = StdOutReceived;
            if (received != null) received(this, e);
        }

        private void InvokeStdErrReceived(DataReceivedEventArgs e)
        {
            DataReceivedHandler received = StdErrReceived;
            if (received != null) received(this, e);
        }

        #endregion

    }
}
